import { defineStore } from 'pinia'

interface QuestionHistoryItem {
  id: string
  fileName: string
  fileSize: number
  htmlContent: string
  timestamp: number
}

export const useQuestionStore = defineStore('question', {
  state: () => ({
    isGenerating: false,
    currentHtmlContent: '',
    history: [] as QuestionHistoryItem[]
  }),
  
  actions: {
    setGenerating(status: boolean) {
      this.isGenerating = status
    },
    
    setCurrentHtmlContent(content: string) {
      this.currentHtmlContent = content
    },
    
    addToHistory(fileName: string, fileSize: number, htmlContent: string) {
      const id = Date.now().toString()
      
      this.history.unshift({
        id,
        fileName,
        fileSize,
        htmlContent,
        timestamp: Date.now()
      })
      
      // 保存到本地存储
      this.saveHistoryToLocalStorage()
    },
    
    removeHistoryItem(id: string) {
      const index = this.history.findIndex(item => item.id === id)
      if (index !== -1) {
        this.history.splice(index, 1)
        this.saveHistoryToLocalStorage()
      }
    },
    
    clearHistory() {
      this.history = []
      localStorage.removeItem('question_history')
    },
    
    saveHistoryToLocalStorage() {
      localStorage.setItem('question_history', JSON.stringify(this.history))
    },
    
    loadHistoryFromLocalStorage() {
      const savedHistory = localStorage.getItem('question_history')
      if (savedHistory) {
        try {
          this.history = JSON.parse(savedHistory)
        } catch (e) {
          console.error('加载历史记录失败:', e)
          this.history = []
        }
      }
    }
  }
}) 